# -*- coding: utf-8 -*-
from django.db import models
from datetime import datetime
from apps.basic.models import *
from apps.prewarning.conf import *

class ModelName(models.Model):
    code = models.CharField(u'业务模型编码', max_length=20, unique=True)
    name = models.CharField(u'业务模型名称', max_length=40, unique=True)
    unit = models.CharField(u'单位', max_length=20)
    remarks = models.TextField(u'说明', null=True, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = '预警|业务模型名称'
        verbose_name_plural = verbose_name


class ModelSet(models.Model):
    model = models.ForeignKey(ModelName, verbose_name=u'业务模型名称',
            related_name='modelsets', unique=True)
    value = models.CharField('阀值', max_length=20, null=True, blank=True)
    radius = models.FloatField('影响半径', max_length=20, null=True, blank=True)
    remarks = models.TextField(u'说明', null=True, blank=True)
    
    def __unicode__(self):
        return self.value

    class Meta:
        verbose_name = '预警|业务模型设置'
        verbose_name_plural = '预警|业务模型设置(ModelSet)'
    
class AnalysisResult(models.Model):
    modelset = models.ForeignKey(ModelSet, verbose_name='模型名称', 
            help_text='低温区,高温区,导线温度上限...')
    area = models.CharField('所处区域', max_length=40, choices=AREA)
    stations = models.ForeignKey(Station, verbose_name='变电站', null=True, blank=True)
    lines = models.ForeignKey(Line, verbose_name='输电线路', null=True, blank=True)
    towers = models.ForeignKey(Tower, verbose_name='杆塔', null=True, blank=True)
    summary = models.TextField('分析结果简介', null=True, blank=True)
    
    def __unicode__(self):
        return 's:%s,l:%s,t:%s' % (self.stations, self.lines, self.towers)

    class Meta:
        verbose_name = '预警|预警分析：分析结果'
        verbose_name_plural = '预警|预警分析：分析结果(AnalysisResult)'


class IrTemperatureMonitor(models.Model):
    code = models.CharField('红外测温编码', max_length=20)
    name = models.CharField('监测点', max_length=40)
    stations = models.ForeignKey(Station, verbose_name='变电站')
    switch_cupboard = models.CharField('开关柜', max_length=20, choices=SWITCH_CUPBOARD,
            null=True, blank=True)
    collect_time = models.DateTimeField('采集时间', default=datetime.now())
    value = models.CharField('红外测温值', max_length=20)
    
    def __unicode__(self):
        return 'k:%s,v:%s' % (self.name, self.value)

    class Meta:
        verbose_name = '预警|在线监测：红外测温'
        verbose_name_plural = '预警|在线监测：红外测温(IrTemperatureMonitor)'


class IrImagingMonitor(models.Model):
    code = models.CharField('红外成像编码', max_length=20)
    name = models.CharField('红外成像名称', max_length=40)
    stations = models.ForeignKey(Station, verbose_name='变电站')
    videography = models.CharField('摄像头', max_length=20, choices=VIDEOGRAPHY,
            null=True, blank=True)
    collect_time = models.DateTimeField('采集时间', default=datetime.now())    
    value = models.CharField('红外成像值', max_length=20)
    image = models.ImageField('红外成像图', upload_to='upload_files/%Y/%m/%d',
            null=True, blank=True)
    
    def __unicode__(self):
        return "k:%s,v:%s" % (self.name, self.value)
    
    class Meta:
        verbose_name = '预警|在线监测：红外成像'
        verbose_name_plural = '预警|在线监测：红外成像(IrImaging)'


class WaveMonitor(models.Model):
    code = models.CharField('谐波编码', max_length=20)
    name = models.CharField('谐波名称', max_length=40)
    stations = models.ForeignKey(Station, verbose_name='变电站')
    collect_time = models.DateTimeField('采集时间', default=datetime.now())        
    value = models.CharField('谐波值', max_length=20)
    
    def __unicode__(self):
        return "k:%s,v:%s" % (self.name, self.value)

    class Meta:
        verbose_name = '预警|在线监测：谐波'
        verbose_name_plural = '预警|在线监测：谐波(WaveMonitor)'


class AtmosphereMonitor(models.Model):
    code = models.CharField('微气象编码', max_length=20)
    name = models.CharField('监测点', max_length=40)
    towers = models.ForeignKey(Tower, verbose_name='安装位置',
            null=True, blank=True, help_text='')
    collect_time = models.DateTimeField('采集时间', default=datetime.now())        
    temperature_value = models.FloatField('温度', max_length=20, null=True, blank=True)
    wet_value = models.FloatField('湿度', max_length=20, null=True, blank=True)
    wind_value = models.FloatField('风速', max_length=20, null=True, blank=True)
    wind_direction = models.FloatField('风向', max_length=20, null=True, blank=True)
    
    def __unicode__(self):
        return 'k:%s,v:%s' % (self.name, self.towers)
    
    class Meta:
        verbose_name = '预警|在线监测：微气象'
        verbose_name_plural = '预警|在线监测：微气象(AtomsphereMonitor)'


class LeadTemperatureMonitor(models.Model):
    code = models.CharField('导线温度编码', max_length=20)
    name = models.CharField('监测点', max_length=40)
    towers = models.ForeignKey(Tower, verbose_name='安装位置',
            null=True, blank=True, help_text='杆塔')
    collect_time = models.DateTimeField('采集时间', default=datetime.now())        
    temperature_value = models.FloatField('导线温度', max_length=20, null=True, blank=True)
    
    def __unicode__(self):
        return 'k:%s,v:%s' % (self.name, self.towers)
    
    class Meta:
        verbose_name = '预警|在线监测：导线温度'
        verbose_name_plural = '预警|在线监测：导线温度(LeadTemperatureMonitor)'


class SaltMonitor(models.Model):
    code = models.CharField('盐密值编码', max_length=20)
    name = models.CharField('监测点', max_length=40)
    towers = models.ForeignKey(Tower, verbose_name='安装位置',
            null=True, blank=True, help_text='杆塔')
    collect_time = models.DateTimeField('采集时间', default=datetime.now())        
    salt_value = models.FloatField('盐密值', max_length=20, null=True, blank=True)
    
    def __unicode__(self):
        return 'k:%s,v:%s' % (self.name, self.towers)
    
    class Meta:
        verbose_name = '预警|在线监测：盐密值'
        verbose_name_plural = '预警|在线监测：盐密值(SaltMonitor)' 
